package com.fourtwoeight.imaging;

import java.io.BufferedReader;
import java.io.InputStreamReader;

public class ImageMagickThumbnailProcessor implements ThumbnailProcessor {

	private String sourceFileName;

	public ImageMagickThumbnailProcessor(String sourceFile) {
		sourceFileName = sourceFile;
	}

	public void scaleToFit(int width, int height, String imageFile)
			throws Exception {
		try {

			/**
			 * mogrify -format gif -path thumbs -thumbnail 100x100 *.jpg
			 */
			String params[] = { "/sw/bin/convert", imageFile,
								"-thumbnail", 
								Integer.toString(width) + "x" + Integer.toString(height), sourceFileName,
								imageFile };
			Process converter = Runtime.getRuntime().exec(params);
			BufferedReader reader = new BufferedReader(new InputStreamReader(
					converter.getInputStream()));
			String lineRead = null;
			while ((lineRead = reader.readLine()) != null) {
				System.out.println(lineRead); // You can send output from here
												// to a GUI display if needed
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}
